package com.example.controller;


import com.example.entity.*;

import com.example.service.HotelService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.List;

@Controller
public class MyController {
    @Autowired
    private HotelService hotelService;


    //首页
    @RequestMapping ("/")
    public String  queryLun(Model model) {
        List<Lunbo> lunbos = hotelService.queryLun();
        model.addAttribute("s",lunbos);
        List<Room> rooms = hotelService.queryRoom();
        model.addAttribute("r",rooms);
        return "index";
    }
    //管理页面
    @RequestMapping ("html/admin")
    public String  admin(Model model) {

        List<User> users = hotelService.queryUs();
        model.addAttribute("u",users);
        List<Ud> uds = hotelService.queryDd();
        model.addAttribute("d",uds);
        List<Fjh> fjhs = hotelService.queryFj();
        model.addAttribute("f",fjhs);
        List<Room> rooms = hotelService.queryRoom();
        model.addAttribute("r",rooms);
        return "html/admin";
    }
    //无权限
    @RequestMapping ("/shou")
    public String  quan(Model model) {

        return "html/shou";
    }
    //预定页面
    @RequestMapping("html/yuding")
    public String yuding(Model model){
        List<Room> rooms = hotelService.queryRoom();
        model.addAttribute("r",rooms);
        return "html/yuding";
    }
    //详情页面
    @RequestMapping("html/xiang/{id}")
    public String xiang(Model model,@PathVariable("id")Integer id){
        List<Room> rooms = hotelService.queryRid(id);
        model.addAttribute("x",rooms);
        return "html/xiang";
    }

    //订单页面
    @RequestMapping("html/ding/{id}")
    public String ding(Model model,@PathVariable("id")Integer id){
        List<Room> rooms = hotelService.queryRid(id);

        model.addAttribute("d",rooms);
        List<Fjh> fjhs = hotelService.queryF(id);
        model.addAttribute("f",fjhs);

       if(rooms==null && fjhs==null){
           return "html/dingdan";
       }

        return "html/dingdan";
    }

    //订单成功后
    @RequestMapping("html/dingh")
    public String up(Fjh fjh, String uid, Date ru, Date tui, String day, String pri,Model model,String rooname){
        int i = hotelService.updateF(fjh);
        hotelService.insertUd(fjh.getFh(),uid,fjh.getRu(),fjh.getTui(),fjh.getDay(),fjh.getPri(),rooname);
        List<Ud> uds = hotelService.queryUd(uid);
        model.addAttribute("ud",uds);
        hotelService.updateR("1",fjh.getFh());
        return "html/mt";

    }

    @RequestMapping("/login")
    public String login(){
        return "html/login";
    }

    @RequestMapping("/loginl")
    public String login2(User user,Model model){

        User user1 = hotelService.queryU(user.getName());
      /*  System.out.println(user1.getId());*/
        //获取当前的用户
        Subject subject = SecurityUtils.getSubject();

        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(user.getName(), user.getPassword());

        try {
            subject.login(token);
            Session session = subject.getSession();
            session.setAttribute("loginuser",user);
//            session.setAttribute("id",user1.getId()+"jjjjjjjjjjj");
            if("111".equals(user.getPassword())&&"管理员".equals(user.getName())){
                   session.setAttribute("perms","admin");
            }else{
                session.setAttribute("perms",null);
            }


            return "redirect:/";

        }catch (UnknownAccountException e){//用户名不存在
            model.addAttribute("msg","用户名错误");
            return "html/login";
        }catch (IncorrectCredentialsException e){//密码不存在

            model.addAttribute("msg","密码错误");
            return "html/login";
        }

    }
    //注销用户
    @RequestMapping("/loginout")
    public String login2(HttpSession session){

        //获取当前的用户
        Subject subject = SecurityUtils.getSubject();

        subject.logout();

        return "html/login";

    }


    //去注册的页面

    @RequestMapping ("/zhu")
    public String  zhu(Model model) {

        List<User> users = hotelService.queryUs();
        model.addAttribute("u",users);
        return "html/register";
    }

    //注册/添加用户
    @RequestMapping ("/zhuu")
    public String  zhuu(User user) {

        int i = hotelService.insertU(user);

        if(i>0){
            return "html/login";
        }else {
            return "html/register";
        }

    }

    //删除用户
    @RequestMapping("/deletU/{id}")
    public String deletU(Model model,@PathVariable("id")Integer id){

         hotelService.deletU(id);
        return "redirect:/html/admin";
    }
    //删除房间
    @RequestMapping("/deletR/{id}")
    public String deletR(@PathVariable("id")Integer id){

        hotelService.deletR(id);
        return "redirect:/html/admin";
    }  //删除房间号
    @RequestMapping("/deletF/{id}")
    public String deletF(@PathVariable("id")Integer id){

        hotelService.deletF(id);
        return "redirect:/html/admin";
    }  //删除订单

     @RequestMapping("deletD/{id}")
    public String  deletD(@PathVariable("id")Integer id){

        int i = hotelService.deletD(id);
        hotelService.queryDd();
        return "redirect:/html/admin";

    }
    //去添加用户的页面
    @RequestMapping ("/insertUu")
    public String  insertUu() {
        return "html/addU";
    }

    //添加用户
    @RequestMapping ("/insertU")
    public String  insertU(User user) {

        int i = hotelService.insertUu(user);
        if(i>0){
            return "redirect:/html/admin";
        }else {
            return "html/addU";
        }
    }

    //跳转到用户修改页面
    @RequestMapping ("/selectUU/{id}")
    public String  selectUU(Model model,@PathVariable("id")Integer id) {

        List<User> users = hotelService.selectUU(id);
        model.addAttribute("u",users);
        return "html/updateU";
    }
    //修改用户
    @RequestMapping ("/updateUU/{id}")
    public String updateUU(User user) {

        int i = hotelService.updateUU(user);
        if(i>0){
            return "redirect:/html/admin";
        }else {
            return "redirect:/selectUU/{id}";
        }
    }

    //去添加房间的页面
    @RequestMapping ("/insertRR")
    public String  insertRR() {
        return "html/addR";
    }
    //添加房间
    @RequestMapping ("/insertRr")
    public String  insertRr(Room room) {

        int i = hotelService.insertR(room);
        if(i>0){
            return "redirect:/html/admin";
        }else {
            return "html/addR";
        }
    }

    //去修改房间的页面
    @RequestMapping ("/selectRRr/{id}")
    public String  selectRRr(Model model,@PathVariable("id")Integer id) {

        List<Room> r = hotelService.selectRRr(id);
        model.addAttribute("r",r);
        return "html/updateR";
    }
    //修改房间
    @RequestMapping ("/updateRR/{id}")
    public String updateRR(Room room) {

        int i = hotelService.updateRR(room);
        if(i>0){
            return "redirect:/html/admin";
        }else {
            return "redirect:/selectRRr/{id}";
        }
    }

    //去添加房间号的页面
    @RequestMapping ("/insertFf")
    public String  insertF() {
        return "html/addF";
    }
    //添加房间
    @RequestMapping ("/insertfF")
    public String  insertRr(Fjh fjh) {

        int i = hotelService.insertF(fjh);
        if(i>0){
            return "redirect:/html/admin";
        }else {
            return "html/addF";
        }
    }
    //根据用户id查询所下订单
    @RequestMapping ("html/selectUid/{id}")
    public String  insertRr(@PathVariable("id")Integer id,Model model) {


        List<Ud> uds = hotelService.selectUId(id);
        model.addAttribute("ud",uds);
        return "html/mt";

    }














}
